;export default function () {
    $(".em-sub").on("click", function () {
        var num = parseInt($("#buyNum").attr("value"));
        // console.log(num);
        if (num === 1) return false;
        num--;
        $("#buyNum").attr("value", num);
        $("#buyNum").val(num);
    });
    $(".em-add").on("click", function () {
        var num = parseInt($("#buyNum").attr("value"));
        num++;
        $("#buyNum").attr("value", num);
        $("#buyNum").val(num);
    });
    $("#buyNum").on("change", function () {
        // console.log($("#buyNum").val());
        if ($("#buyNum").val() === "") {
            alert("购买数量不能为空!");
            $("#buyNum").attr("value", 1);
            $("#buyNum").val(1);
        }
        else {
            $("#buyNum").attr("value", $("#buyNum").val());
        }
    });
    $("#buy_Now").on("click", function () {
        var id = parseInt(window.location.hash.replace("#id=", ""));
        var count = parseInt($("#buyNum").attr("value"));
        var ls = localStorage.getItem("cart");
        if (ls === "") {
            var item = [{ id, count }];
            localStorage.setItem("cart", JSON.stringify(item));
        }
        else {
            var ls_list = JSON.parse(ls);
            var has_same_id = false;
            $.each(ls_list, function (index, item) {
                if (item.id == id) {
                    item.count += count;
                    has_same_id = true;
                }
            });
            if (!has_same_id) {
                ls_list.push({ id, count });
            }
            localStorage.setItem("cart", JSON.stringify(ls_list));
        }
        $(".js-cart-num").html($(".js-cart-num").html() * 1 + count);
    });
    $(".mini-cart").on("mouseenter", function () {
        $(".cart-wrap").addClass("mini-cart-over");
        // console.log(1);
        renderCart();
    });
    $(".cart-wrap").on("mouseleave", function () {
        $(this).removeClass("mini-cart-over");
        $(".li-total").siblings().remove("li");
    });
    $(".ul-mini-cart-list").on("click", ".s-del", function () {
        var del_id = $(this).parent().attr("good-id");
        var ls = localStorage.getItem("cart");
        var ls_list = JSON.parse(ls);
        $.each(ls_list, function (index, item) {
            if (item.id == del_id) {
                ls_list.splice(index, 1);
                return false;
            }
        });
        if (ls_list.length === 0) {
            // console.log("空了");
            localStorage.setItem("cart", "");
            renderCart();
        }
        else {
            localStorage.setItem("cart", JSON.stringify(ls_list));
            $(".li-total").siblings().remove("li");
            renderCart();
        }
    });
    $(".cart-wrap").on("click", ".clear-cart", function () {
        // console.log("clear!");
        localStorage.setItem("cart", "");
        renderCart();
    });
    function renderCart() {
        var ls = localStorage.getItem("cart");
        // console.log(ls);
        if (ls !== "") {
            var ls_list = JSON.parse(ls);
            var html = "";
            var index_id = 0;
            var total_count = 0;
            var total_price = 0;
            $.each(ls_list, function (index, item) {
                var good = item;
                $.each(window.data, function (index, item) {
                    if (good.id == item.productId) {
                        index_id = index;
                        return false;
                    }
                });
                html += `<li good-id="${window.data[index_id].productId}">
                                    <a href="http://localhost:8888/html/detail.html#id=${window.data[index_id].productId}" class="a-thumb js_link"><img src=${window.data[index_id].cartImg} class="js_thumb" width="50" height="50"></a>
                                    <a href="http://localhost:8888/html/detail.html#id=${window.data[index_id].productId}" class="a-title js_link">${window.data[index_id].productName}</a>
                                    <span class="s-price">¥
                                        <em class="em-price">${window.data[index_id].price}</em> x 
                                        <em class="em-num">${good.count}</em>
                                    </span>
                                    <span class="s-del" title="删除此商品">删除</span>
                                </li>`;
                total_count += item.count;
                total_price += item.count * window.data[index_id].price;
                // console.log(item.count,window.data[index_id].price);
            });
            // console.log(total_price);
            $(".cart-empty-tip").attr("style", "display:none;");
            $(".ul-mini-cart-list").attr("style", "display:block;");
            $(".goods-list").html(html);
            // $(".ul-mini-cart-list").html(html + $(".ul-mini-cart-list").html());
            $(".js-cart-num").html(total_count);
            $(".em-total-price").html(total_price);
        }
        else {
            $(".cart-empty-tip").attr("style", "display:block;");
            $(".ul-mini-cart-list").attr("style", "display:none;");
            $(".js-cart-num").html(0);
        }
    }
}